OBJ_DIR := obj
SRC_DIR := src
BIN_DIR := bin
INCLUDE_DIR := include
DEPENDENCY_DIR := dependency

CXX := gcc
CXXFLAGS := -std=c11 -g -Wall -I./$(INCLUDE_DIR) -DDEBUG
LDLIBS := -lm

SRCS := $(wildcard $(SRC_DIR)/*.c)
HDRS := $(wildcard $(INCLUDE_DIR)/*.h)
OBJS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS))
DEPENDENCIES:= $(patsubst $(SRC_DIR)/%.c,$(DEPENDENCY_DIR)/%.mk,$(SRCS))
TARGET := $(BIN_DIR)/yolov3_preprocessing

.DEFAULT_GOAL := $(TARGET)


include $(DEPENDENCIES)

$(TARGET): $(OBJS)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDLIBS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -c -o $@ $< $(LDLIBS)

$(DEPENDENCY_DIR)/%.mk: $(SRC_DIR)/%.c
	@mkdir -p $(DEPENDENCY_DIR)
	@set -e; rm -f $@; \
		$(CXX) -MM $(CXXFLAGS) $< > $@.$$$$; \
		sed -E 's,($*)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' < $@.$$$$ > $@; \
		rm -f $@.$$$$

.PHONY: clean
clean:
	rm -rf $(OBJS)
	rm -rf $(DEPENDENCIES)
